package com.woniu.gf.pawn.gateway.filter;

import com.woniu.gf.pawn.comm.core.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: spring-cloud
 * @BelongsPackage: com.woniu.cq.gateway.filter
 * @Author: wu wei
 * @CreateTime: 2020-09-22 14:16
 * @Description:
 */
@Component
public class AuthenFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("权限认证");
        String uri = exchange.getRequest().getPath().toString();
        System.out.println(uri);
        List<String> list = Arrays.asList(
                ("/management/v2/api-docs"),
                ("/store/v2/api-docs"),
                ("/product/v2/api-docs"),
                ("/product-entity/v2/api-docs"),
                ("/shop/v2/api-docs")
        );
        if(list.contains(uri)){
            return chain.filter(exchange);
        }
        if(uri.endsWith("/")){
            uri=uri.substring(0,uri.length()-1);
        }
        if(uri.startsWith("/management/management/getSmsCode")){
            uri="/management/management/getSmsCode";
        }
        if(uri.startsWith("/store/store/deleteStore")){
            uri="/store/store/deleteStore";
        }

        if(uri.startsWith("/shop/shop/findOneShopInfo")){
            uri="/shop/shop/findOneShopInfo";
        }
        if(uri.startsWith("/shop/shop/deleteShop")){
            uri="/shop/shop/deleteShop";
        }
        if(uri.startsWith("/product-entity/product-entity/findOneProductEntity")){
            uri="/product-entity/product-entity/findOneProductEntity";
        }
        if(uri.startsWith("/product-entity/product-entity/deleteProduct")){
            uri="/product-entity/product-entity/deleteProduct";
        }
        if(uri.startsWith("/product-entity/product-entity/findAllByBigTypeId")){
            uri="/product-entity/product-entity/findAllByBigTypeId";
        }
        if(uri.startsWith("/product/product-big-type/findName")){
            uri="/product/product-big-type/findName";
        }
        if(uri.startsWith("/product/product-big-type/findPage")){
            uri="/product/product-big-type/findPage";
        }
        if(uri.startsWith("/product/product-big-type/findByIdDefinition")){
            uri="/product/product-big-type/findByIdDefinition";
        }
        if(uri.startsWith("/product/product-brand/findPage")){
            uri="/product/product-brand/findPage";
        }
        if(uri.startsWith("/product/product-brand/findById")){
            uri="/product/product-brand/findById";
        }
        if(uri.startsWith("/product/product-brand/findById")){
            uri="/product/product-brand/findById";
        }
        if(uri.startsWith("/product/product-brand/findNotById")){
            uri="/product/product-brand/findNotById";
        }
        if(uri.startsWith("/product/product/findPage")){
            uri="/product/product/findPage";
        }
        if(uri.startsWith("/product/product/findBrandType")){
            uri="/product/product/findBrandType";
        }
        if(uri.startsWith("/product/product-property/findPage")){
            uri="/product/product-property/findPage";
        }
        if(uri.startsWith("/product/product-small-type/findPage")){
            uri="/product/product-small-type/findPage";
        }
        if(uri.startsWith("/store/store/deleteStore")){
            uri="/store/store/deleteStore";
        }
        System.out.println(uri);
        if(uri.equals("/management/management/passwordLogin") || uri.equals("/management/management/getSmsCode")
                || uri.equals("/management/management/smsLogin") || uri.equals("/management/management/alterManagementPassword")
                || uri.equals("/management/management/alterManagementInfo") || uri.equals("/management/management/logout")
                || uri.equals("/management/management/selectManagementInfo")) {
            return chain.filter(exchange);
        }
        String token = exchange.getRequest().getHeaders().getFirst("token");
        if(StringUtils.isEmpty(token)){
            exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
            return exchange.getResponse().writeWith(Mono.fromSupplier(() ->{
                return exchange.getResponse().bufferFactory().wrap("{\"code\":\"403\",\"msg\":\"没有登陆\"}".getBytes());
            }));
        }
        try {
            Map<String, Object> map = JwtUtil.parseToken(token);
            List<String> permissionList=(List<String>) map.get("permissions");
            if(permissionList==null || permissionList.get(0)==null || permissionList.isEmpty()){
                exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
                return exchange.getResponse().writeWith(Mono.fromSupplier(() ->{
                    return exchange.getResponse().bufferFactory().wrap("{\"code\":\"404\",\"msg\":\"没有权限\"}".getBytes());
                }));
            }
            if(permissionList.contains(uri)){
                return chain.filter(exchange);
            }else {
                exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
                return exchange.getResponse().writeWith(Mono.fromSupplier(() ->{
                    return exchange.getResponse().bufferFactory().wrap("{\"code\":\"404\",\"msg\":\"没有权限\"}".getBytes());
                }));
            }
        }catch (Exception e){
            e.printStackTrace();
            exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
            return exchange.getResponse().writeWith(Mono.fromSupplier(() ->{
                return exchange.getResponse().bufferFactory().wrap("{\"code\":\"403\",\"msg\":\"token解析失败\"}".getBytes());
            }));
        }
    }

    @Override
    public int getOrder() {
        return 2;
    }
}
